ig.module(
    
    'game.entities.player'
    
).requires(
    
    'impact.entity',
    'game.entities.bullit',
    'game.entities.particle'
    
).defines(function(){

    EntityPlayer = ig.Entity.extend({
        size: {
            x: 16,
            y: 16
        },
        offset: {
            x: 0,
            y: 0
        },
        maxVel: {
            x: 150,
            y: 150
        },
        shootTimer: null,
        shootPeriod: 0.15,
        target: null,
        gravityFactor: 0,
        type: ig.Entity.TYPE.A,
        checkAgainst: ig.Entity.TYPE.B,
        collides: ig.Entity.COLLIDES.PASSIVE,
        health: 500,
        animSheet: new ig.AnimationSheet('media/sprites/factory.png', 16, 16),
        init: function (x, y, settings) {           
            this.parent(x, y, settings);
            this.addAnim('idle', 1, [0]);            
            this.shootTimer = new ig.Timer();
        },
        update: function () {

            this.friction = 30;
            
            this.vel.x = (ig.input.mouse.x - (this.pos.x + this.size.x/2))*5;
            this.vel.y = (ig.input.mouse.y - (this.pos.y + this.size.y/2))*5;
            
            if(ig.input.state('shoot') && this.shootTimer.delta() > this.shootPeriod){                

                //ig.game.backgroundMaps[0].setTile( this.pos.x, this.pos.y, 3);
                
                var dx = this.target.pos.x + this.target.size.x/2 - (this.pos.x + this.size.x/2);
                var dy = this.target.pos.y + this.target.size.y/2 - (this.pos.y + this.size.y/2);
                
                
                
                ig.game.spawnEntity(EntityBullit,
                                    this.pos.x + this.size.x/2,
                                    this.pos.y + this.size.y/2, {
                                    moveclass: ig.movement.LinearMovement.extend({
                                        vel: ig.VUtils.scaleToLen({x: dx, y: dy}, 100),
                                        accel: ig.VUtils.scaleToLen({x: dx, y: dy}, 600)
                                    })
                    }
                );
                this.shootTimer.set(0);
            }
            
            this.parent();
        },
        kill: function () {
            var gibs = ig.ua.mobile ? 5 : 30;
            for (var i = 0; i < gibs; i++) {
                ig.game.spawnEntity(EntityPlayerGib, this.pos.x, this.pos.y);
            }
            this.parent();
        },
        receiveDamage: function (amount, from) {
            this.parent(amount);
        },
        check: function (other) {
            
        }
    });
    
    EntityPlayerGib = EntityParticle.extend({
        lifetime: 3,
        fadetime: 6,
        friction: {
            x: 0,
            y: 0
        },
        vel: {
            x: 60,
            y: 150
        },
        animSheet: new ig.AnimationSheet('media/sprites/blob-gibs.png', 4, 4),
        init: function (x, y, settings) {
            this.addAnim('idle', 0.1, [0, 1, 2]);
            this.parent(x, y, settings);
        }
    });

})